package com.krmes.mes.base;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Build.VERSION;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.krmes.mes.R;

@SuppressLint({"InflateParams"})
public class BaseApplication
        extends Application
{
    private static String PREF_NAME = "creativelocker.pref";
    static Context _context;
    private static String lastToast = "";
    private static long lastToastTime;
    private static boolean sIsAtLeastGB;

    static
    {
        if (VERSION.SDK_INT >= 9) {
            sIsAtLeastGB = true;
        }
    }
    @Override
    public void onCreate()
    {
        super.onCreate();
        _context = this.getApplicationContext();
    }
    @TargetApi(9)
    public static void apply(Editor paramEditor)
    {
        if (sIsAtLeastGB)
        {
            paramEditor.apply();
            return;
        }
        paramEditor.commit();
    }
    public static synchronized BaseApplication context() {
        BaseApplication baseApplication;
        synchronized (BaseApplication.class) {
            baseApplication = (BaseApplication) _context;
        }
        return baseApplication;
    }

    @TargetApi(11)
    public static SharedPreferences getPreferences()
    {
        return context().getSharedPreferences(PREF_NAME, 4);
    }

    @TargetApi(11)
    public static SharedPreferences getPreferences(String paramString)
    {
        return context().getSharedPreferences(paramString, 4);
    }

    public static void showToast(int paramInt)
    {
        showToast(paramInt, 0, 0);
    }

    public static void showToast(int paramInt1, int paramInt2)
    {
        showToast(paramInt1, 0, paramInt2);
    }

    public static void showToast(int paramInt1, int paramInt2, int paramInt3)
    {
        showToast(paramInt1, paramInt2, paramInt3, 80);
    }

    public static void showToast(int paramInt1, int paramInt2, int paramInt3, int paramInt4)
    {
        showToast(context().getString(paramInt1), paramInt2, paramInt3, paramInt4);
    }

    public static void showToast(int paramInt1, int paramInt2, int paramInt3, int paramInt4, Object... paramVarArgs)
    {
        showToast(context().getString(paramInt1, paramVarArgs), paramInt2, paramInt3, paramInt4);
    }

    public static void showToast(String paramString)
    {
        showToast(paramString, 0, 0, Gravity.CENTER);
    }

    public static void showToast(String paramString, int paramInt)
    {
        showToast(paramString, 0, paramInt, Gravity.CENTER);
    }

    public static void showToast(String message, int duration, int icon, int gravity)
    {
        if((message != null) && (!message.equalsIgnoreCase(""))) {
            long time = System.currentTimeMillis();
            if((!message.equalsIgnoreCase(lastToast)) || (Math.abs((time - lastToastTime)) > 0x7d0)) {
                View view = LayoutInflater.from(context()).inflate(R.layout.view_toast, null);
                ((TextView)view.findViewById(R.id.title_tv)).setText(message);
                if(icon != 0) {
                    ((ImageView)view.findViewById(R.id.icon_iv)).setImageResource(icon);
                    ((ImageView)view.findViewById(R.id.icon_iv)).setVisibility(View.VISIBLE);
                }
                Toast toast = new Toast(context());
                toast.setView(view);
                if(gravity == 0x11) {
                    toast.setGravity(gravity, 0x0, 0x0);
                } else {
                    toast.setGravity(gravity, 0x0, 0x23);
                }
                toast.setDuration(duration);
                //toast.setGravity(0x11, 0x0, 0x0);
                toast.show();
                lastToast = message;
                lastToastTime = System.currentTimeMillis();
            }
        }

    }

    public static void showToastLong(int paramInt)
    {
        showToast(paramInt, 1, 0);
    }

    public static void showToastLong(int paramInt, Object... paramVarArgs)
    {
        showToast(paramInt, 1, 0, 80, paramVarArgs);
    }

    public static void showToastLong(String paramString)
    {
        showToast(paramString, 1, 0, 80);
    }


}
